package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize(), "update_time desc");
        Page<Setmeal> setmealPage = setmealMapper.findByPage(setmealPageQueryDTO);
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        log.info("正在添加套餐信息:{}", setmealDTO);
        setmealMapper.addSetmeal(setmealDTO);
        //添加菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        log.info("添加菜品信息:{}", setmealDishes);
        //判断是否有菜品
        if (!ObjectUtil.isNull(setmealDishes) && !ObjectUtil.isEmpty(setmealDishes)) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
            setmealDishMapper.addSetmealDishs(setmealDishes);
        }
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        setmealMapper.updateStatus(status, id);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealMapper.findById(id);
        return setmeal;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Integer> ids) {
        if (!ObjectUtil.isEmpty(ids) && !ObjectUtil.isNull(ids)) {
            setmealMapper.deleteSetmeal(ids);
            setmealDishMapper.deleteSetmealDish(ids);
        }
    }

    /**
     * 修改套餐
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //判断修改后的是否为空
        if (!ObjectUtil.isNull(setmealDTO) && !ObjectUtil.isEmpty(setmealDTO)) {
            setmealMapper.updateSetmeal(setmealDTO);
            //拿到菜品
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            //先删除
            Integer id = Math.toIntExact(setmealDTO.getId());
            List<Integer> list = new ArrayList<>();
            list.add(id);
            setmealDishMapper.deleteSetmealDish(list);
            //在增加
            if (!ObjectUtil.isEmpty(setmealDishes) && !ObjectUtil.isNull(setmealDishes)) {
                setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
                setmealDishMapper.addSetmealDishs(setmealDishes);
            }
        }
    }

    /**
     * C端根据套餐id查询包含的菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> findSetmealDishById(Integer id) {
        List<DishItemVO> dishItemVOS = setmealDishMapper.findSetmealDishById(id);
        return dishItemVOS;
    }

    /**
     * C端根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> findByIdC(Integer categoryId) {
        List<Setmeal> setmeals = setmealMapper.findByIdC(categoryId);
        return setmeals;
    }
}
